package org.example.market_spring.controller;

import org.example.market_spring.entity.Goods;
import org.example.market_spring.entity.SaleTicket;
import org.example.market_spring.service.GoodsService;
import org.example.market_spring.service.IndexService;
import org.example.market_spring.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @PostMapping("/queryGoods")
    public List<Goods> queryGoods(@RequestBody Goods goods){
        return goodsService.queryGoods(goods);
    }

    @GetMapping("/queryByIds")
    public List<Goods> queryByIds(String[] id){

        return goodsService.queryByIds(id);
    }

    @PostMapping("/queryFAMsg")
    public List<SaleTicket> queryFAMsg(@RequestBody SaleTicket saleTicket){
        return goodsService.queryFAMsg(saleTicket);
    }





    @Autowired
    private IndexService indexService;

    @GetMapping("/new/goodsBySale")
    public List<Goods> goodsBySale(String id,String goodsName,String goodsType){
        return indexService.goodsBySale(id,goodsName,goodsType);
    }


    @PostMapping("/updateGoodsNum")
    public void updateGoodsNum(@RequestParam(value = "id") String[] id, @RequestBody  Goods goods){
        indexService.updateGoodsNum(id,goods);
    }

    @GetMapping("/querySaleTableData")
    public List<Goods> querySaleTableData(String[] id){
        return indexService.querySaleTableData(id);
    }

    @PostMapping("/addSaleTicket")
    public void addSaleTicket(@RequestBody SaleTicket saleTicket){
        indexService.addSaleTicket(saleTicket);
    }

    //销售单据查询
    @PostMapping("/querySaleTicket")
    public List<SaleTicket> querySaleTicket(@RequestBody SaleTicket saleTicket){
        return indexService.querySaleTicket(saleTicket);
    }

    @GetMapping("/queryOutTicket")
    public R queryOutTicket(String[] id){

        List<Goods> goodsList = indexService.compareToSaleNumAndStoreNum(id);
        String [] ids={goodsList.get(0).getId()};
        //销售数量 - 库存数量
        if(goodsList.get(0).getStoreNum() - goodsList.get(0).getSaleNum()>=0){
            //设置出库数量
            Goods goods = new Goods();
            goods.setSaleNum(goodsList.get(0).getSaleNum());
            goods.setStoreNum(goodsList.get(0).getStoreNum());
//            indexService.updateGoodsNum(ids,0,goodsList.get(0).getSaleNum(),goodsList.get(0).getStoreNum());
            indexService.updateGoodsNum(ids,goods);
//            //生成出库单
            List<Goods> list = indexService.queryOutTableData(ids);
            return R.ok("出库单").put("list",list);
        }else{
            //生成采购信息表
            //生成缺货单
            List<Goods> list = indexService.queryOutTableData(ids);
            return R.ok("库存不足").put("list",list);

        }
    }

    @GetMapping("/queryFaTicket")
    public R queryFaTicket(String[] id){
        List<Goods> list = indexService.queryFaTicket(id);
        return R.ok("查询成功").put("list",list);
    }

    //查询库存信息
    @GetMapping("/queryGoodsStoreMsg")
    public List<Goods> queryGoodsStoreMsg(String id){
        return indexService.queryGoodsStoreMsg(id);
    }

    //缺货单查询
    @PostMapping("queryQHGoodsStoreMsg")
    public List<Goods> queryQHGoodsStoreMsg(@RequestBody  Goods goods){
        return indexService.queryQHGoodsStoreMsg(goods);
    }

    //订货单查询
    @PostMapping("queryBookStoreMsg")
    public List<Goods> queryBookStoreMsg(@RequestBody Goods goods){
        return indexService.queryBookStoreMsg(goods);
    }

    @GetMapping("/queryBookTicket")
    public R queryBookTicket(String[] id){
        List<Goods> list = indexService.queryBookTicket(id);
        return R.ok("查询成功").put("list",list);
    }

    //收货单查询
    @PostMapping("queryIntoStoreMsg")
    public List<Goods> queryIntoStoreMsg(@RequestBody Goods goods){
        return indexService.queryBookStoreMsg(goods);
    }

    @GetMapping("/queryIntoTicket")
    public R queryIntoTicket(String[] id){
        List<Goods> list = indexService.queryIntoTicket(id);
        return R.ok("查询成功").put("list",list);
    }
}
